package com.bawei.monthdemo2207a.month02.home

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.ShoppingCart
import androidx.compose.material.icons.filled.Star
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.bawei.monthdemo2207a.R
import com.king.ultraswiperefresh.UltraSwipeRefresh
import com.king.ultraswiperefresh.rememberUltraSwipeRefreshState
import kotlinx.coroutines.delay

/**
 * 作者：赵亮
 * 时间：2024/12/31 11:31
 * 功能：
 */
@Composable
fun HomePage() {

    var searchContent by remember {
        mutableStateOf("中国建设银行")
    }

    var selectIndex by remember {
        mutableStateOf(0)
    }

    val state = rememberUltraSwipeRefreshState()

    var itemCount by remember {
        mutableStateOf(5)
    }

    LaunchedEffect(state.isRefreshing) {
        if (state.isRefreshing) {
            delay(2000)
            itemCount = 5
            state.isRefreshing = false
        }
    }

    LaunchedEffect(state.isLoading) {
        if (state.isLoading) {
            delay(2000)
            itemCount += 5
            state.isLoading = false
        }
    }

    Column(
        Modifier
            .fillMaxSize()
            .padding(top = 24.dp)
    ) {
        // 标题
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(50.dp)
                .padding(start = 20.dp, end = 20.dp)
                .background(color = Color.Gray, shape = RoundedCornerShape(5.dp)),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Spacer(modifier = Modifier.width(10.dp))
            Icon(imageVector = Icons.Default.Search, contentDescription = null)
            BasicTextField(
                modifier = Modifier.weight(1f),
                value = searchContent,
                onValueChange = { searchContent = it })
            Icon(imageVector = Icons.Default.ShoppingCart, contentDescription = null)
            Spacer(modifier = Modifier.width(10.dp))
        }


        // Tab
        TabRow(divider = {}, indicator = {}, selectedTabIndex = selectIndex) {
            listOf("精选", "软件", "游戏", "残局", "小游戏").forEachIndexed { index, s ->
                Tab(
                    selectedContentColor = Color.Black,
                    unselectedContentColor = Color.Gray,
                    selected = selectIndex == index,
                    onClick = { selectIndex = index }) {
                    if (selectIndex == index) {
                        Text(text = s, fontSize = 22.sp)
                    } else {
                        Text(text = s, fontSize = 15.sp)
                    }
                }
            }
        }

        // Tab 对应页面
        when (selectIndex) {
            0 -> {
                UltraSwipeRefresh(state = state,
                    onRefresh = { state.isRefreshing = true },
                    onLoadMore = { state.isLoading = true }) {
                    Column(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(start = 10.dp, end = 10.dp, bottom = 100.dp)
                    ) {
                        // 三行横向列表
                        listOf("", "热门应用", "热门游戏").forEachIndexed { index, s ->
                            if (index != 0) {
                                Text(text = s, fontSize = 22.sp, fontWeight = FontWeight.Bold)
                            }
                            LazyRow(horizontalArrangement = Arrangement.spacedBy(10.dp)) {
                                items(10) {
                                    Column(horizontalAlignment = Alignment.CenterHorizontally) {
                                        Image(
                                            modifier = Modifier
                                                .size(60.dp)
                                                .clip(RoundedCornerShape(15.dp)),
                                            painter = painterResource(id = R.drawable.logo),
                                            contentDescription = null
                                        )
                                        Text(text = "三角洲")
                                    }
                                }
                            }
                        }

                        LazyColumn(modifier = Modifier.fillMaxWidth()) {
                            items(itemCount) {
                                Row(modifier = Modifier.fillMaxWidth()) {
                                    Image(
                                        modifier = Modifier
                                            .size(60.dp)
                                            .clip(RoundedCornerShape(15.dp)),
                                        painter = painterResource(id = R.drawable.logo),
                                        contentDescription = null
                                    )
                                    Column(modifier = Modifier.weight(1f)) {
                                        Text(text = "QQ飞车")
                                        Text(text = "腾讯首款3D竞速手游 ")
                                        Text(text = "QQ飞车")
                                    }
                                    Button(onClick = { /*TODO*/ }) {
                                        Text(text = "下载")
                                    }
                                }
                            }
                        }
                    }
                }

            }

            1 -> {
                Column {
                    LazyRow (horizontalArrangement = Arrangement.spacedBy(10.dp)){
                        items(10) {
                            Column {
                                Image(
                                    painter = painterResource(id = R.drawable.logo_weixin),
                                    contentDescription = null,
                                    modifier = Modifier.size(60.dp)
                                )
                                Text(text = "小程序")
                            }
                        }
                    }
                    LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp)) {
                        items(10) {
                            Row (modifier = Modifier.fillMaxWidth()){
                                Image(
                                    modifier = Modifier.size(60.dp),
                                    painter = painterResource(id = R.drawable.logo_weixin),
                                    contentDescription = null
                                )
                                Column (modifier = Modifier.weight(1f)){
                                    Text(text = "微信")
                                    Text(text = "微信，是一个生活方式")
                                }
                                Button(onClick = { /*TODO*/ }) {
                                    Text(text = "下载")
                                }
                            }

                        }
                    }
                }
            }

            2 -> Text(text = "游戏")
            3 -> Text(text = "残局")
            4 -> Text(text = "小游戏")
        }
    }
}

@Preview
@Composable
fun HomePagePreview() {
    HomePage()
}

